home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lzw4c11.zip / TEST_LZW.C < prev    next >
Text File  |  1992-11-08  |  3KB  |  124 lines

  1. /*
  2. **   TEST_LZW.C       Copyright (C) 1992 by MarshallSoft Computing, Inc.
  3. **
  4. **   This program is used to compress, expand, and verify each specified
  5. **   file. It's purpose is for you to test the LZW4C library on your own
  6. **   files. Your files are never modified. However, you should NOT have a
  7. **   file named "XXX.XXX" or "YYY.YYY".  Compression ratios are printed
  8. **   for each file compressed. For example, to compress all files ending
  9. **   in *.C in your current directory, type:
  10. **
  11. **        TEST_LZW *.c
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <dos.h>
  16. #include <fcntl.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <io.h>
  20.  
  21. #include "LZW4C.H"
  22. #include "RW_IO.H"
  23. #include "DIR_IO.H"
  24.  
  25. extern char *malloc();
  26. extern int free();
  27.  
  28. void SayError(int);
  29.  
  30. FILE *FilePtr1;
  31. FILE *FilePtr2;
  32.  
  33. void main(argc,argv)
  34. int argc;
  35. char *argv[];
  36. {int i, k;
  37.  int x, y;
  38.  int RetCode;
  39.  float Ratio;
  40.  long Index;
  41.  char Filename[15];
  42.  int Files = 0;
  43.  /* begin */
  44.  if(argc!=2)
  45.    {printf("Usage: TEST_LZW <filespec>\n");
  46.     exit(1);
  47.    }
  48.  RetCode = InitLZW(malloc);
  49.  if(RetCode<0)
  50.    {SayError(RetCode);
  51.     exit(2);
  52.    }
  53.  /* flush the keyboard */
  54.  while(kbhit()) getchar();
  55.  puts("\nTEST_LZW 1.0: Type any key to abort...");
  56.  for(i=0;;i++)
  57.    {if(kbhit())
  58.       {puts("\n...Aborted by user !");
  59.        break;
  60.       }
  61.     if(i==0) RetCode = FindFirst(argv[1],Filename);
  62.     else RetCode = FindNext(Filename);
  63.     if(!RetCode) break;
  64.     /* get pointer to file */
  65.     for(k=0;k<strlen(Filename);k++) Filename[i] = toupper(Filename[i]);
  66.     if( (strcmp(Filename,"XXX.XXX")==0) ||
  67.         (strcmp(Filename,"YYY.YYY")==0) ) continue;
  68.     /* open input file for compression */
  69.     if(!ReaderOpen(Filename)) exit(3);
  70.     /* open output file for compression */
  71.     if(!WriterOpen("XXX.XXX")) exit(4);
  72.     /* do the compression */
  73.     Files++;
  74.     printf("\nCompressing %12s ",Filename);
  75.     if((RetCode=Compress(Reader,Writer))<0)
  76.       {SayError(RetCode);
  77.        exit(5);
  78.       }
  79.     /* report compression ratio */
  80.     if(ReaderCount() > 0)
  81.        {Ratio = (float)(WriterCount())/(float)ReaderCount();
  82.         printf(" OK (%0.2f)\n",Ratio);
  83.        }
  84.     else puts("???");
  85.     /* close files */
  86.     ReaderClose();
  87.     WriterClose();
  88.     /* open input file for expansion */
  89.     if(!ReaderOpen("XXX.XXX")) exit(6);
  90.     /* open output file */
  91.     if(!WriterOpen("YYY.YYY")) exit(7);
  92.     /* do the expansion */
  93.     printf("  Expanding %12s ",Filename);
  94.     if((RetCode=Expand(Reader,Writer))<0)
  95.       {printf("Expand returns error %d\n",RetCode);
  96.       }
  97.     /* close files */
  98.     ReaderClose();
  99.     WriterClose();
  100.     printf(" OK\n");
  101.     /* compare original to expanded file */
  102.     FilePtr1 = fopen(Filename,"rb");
  103.     FilePtr2 = fopen("YYY.YYY","rb");
  104.     printf("  Comparing              ");
  105.     Index = 0;
  106.     while(1)
  107.       {x = fgetc(FilePtr1);
  108.        y = fgetc(FilePtr2);
  109.        Index++;
  110.        if((Index&0x0fff)==0) putchar('.');
  111.        if((x==EOF)&&(y==EOF)) break;
  112.        if(x!=y)
  113.          {printf("Difference between files at index %ld\n",Index-1);
  114.           exit(8);
  115.          }
  116.       }
  117.     printf(" OK\n");
  118.     fclose(FilePtr1);
  119.     fclose(FilePtr2);
  120.    }
  121.  TermLZW(free);
  122.  printf("\n%d files tested\n",Files);
  123.  exit(0);
  124. }